package com.rlz.lib_new_base.base

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable

/**
 * Created by RLZ
 * on 2021/10/18
 *  IViewModel的实现类
 */
class BaseViewModel : ViewModel(), IViewModel {

    override fun onAny(owner: LifecycleOwner?, event: Lifecycle.Event?) {}
    override fun onCreate() {}
    override fun onDestroy() {}
    override fun onStart() {}
    override fun onStop() {}
    override fun onResume() {}
    override fun onPause() {}


    val loadingChange: LoadingChange by lazy { LoadingChange() }

    /**
     * 管理RxJava
     */
    private var mCompositeDisposable: CompositeDisposable? = null

    protected fun addSubscribe(disposable: Disposable) {
        if (mCompositeDisposable == null)
            mCompositeDisposable = CompositeDisposable()
        mCompositeDisposable?.add(disposable)
    }

    /**
     * ViewModel 销毁时执行 取消所有的异步任务
     */
    override fun onCleared() {
        super.onCleared()
        if (mCompositeDisposable != null)
            mCompositeDisposable?.clear()
    }


    inner class LoadingChange {

       /* val show by lazy { SingleEvent<String>() }

        val dismiss by lazy { SingleEvent<Boolean>() }*/
    }
}